package com.wee.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigtotoro.UILImageLoader;
import com.bigtotoro.util.SharedPreferencesUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wee.aircoach_user.Constant;
import com.wee.aircoach_user.R;
import com.wee.entity.UserHistory;

import java.util.List;

/**
 * Created by xiaobing on 15/8/17.
 * 教练注册里面， 职业类型，擅长领域
 */
public class MyTestAdapter1 extends BaseAdapter{
    private Context context;
    private List<UserHistory> data;
    private DisplayImageOptions options;
    public MyTestAdapter1(Context context, List<UserHistory> data) {
        this.context = context;
        this.data = data;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.find1_no_dz)
                .showImageForEmptyUri(R.drawable.find1_no_dz )
                .showImageOnFail(R.drawable.find1_no_dz)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
        UILImageLoader.initImageLoader(context.getApplicationContext());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if(convertView==null){
            holder = new ViewHolder();
            LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflate.inflate(R.layout.item_my_test, null);
            holder.history_point=(LinearLayout)convertView.findViewById(R.id.history_point);
            holder.history_lishi=(LinearLayout)convertView.findViewById(R.id.history_lishi);
            holder.fuWO=(LinearLayout)convertView.findViewById(R.id.layout_fuWO);
            holder.defen=(TextView)convertView.findViewById(R.id.defen);
            holder.test0 = (TextView)convertView.findViewById(R.id.test0);
            holder.test1 = (TextView)convertView.findViewById(R.id.test1);
            holder.test2 = (TextView)convertView.findViewById(R.id.test2);
            holder.time = (TextView)convertView.findViewById(R.id.time);
            holder.text_coach = (TextView)convertView.findViewById(R.id.text_coach);
            holder.somename = (TextView)convertView.findViewById(R.id.somename);
            holder.text_style = (TextView)convertView.findViewById(R.id.text_style);
            holder.power = (TextView)convertView.findViewById(R.id.power);
            holder.figure=(ImageView)convertView.findViewById(R.id.figure);
            holder.img_dongzuo=(ImageView)convertView.findViewById(R.id.img_dongzuo);
            holder.buju=(LinearLayout)convertView.findViewById(R.id.buju);
            holder.plan_line=(ImageView)convertView.findViewById(R.id.plan_line);
            holder.rootView = convertView.findViewById(R.id.root_layout);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        UserHistory history = data.get(position);
        holder.rootView.setVisibility(View.GONE);
        /**类型区分**/
        /**登录*/
try{
        if (history.getType().equals("coach")){
            holder.rootView.setVisibility(View.VISIBLE);
            holder.figure.setVisibility(View.VISIBLE);
            holder.history_point.setVisibility(View.GONE);
      //      holder.delete.setVisibility(View.GONE);
            holder.history_lishi.setVisibility(View.VISIBLE);
            holder.buju.setVisibility(View.GONE);
            holder.text_coach.setVisibility(View.VISIBLE);
            holder.plan_line.setImageResource(R.drawable.train_plan);
            ImageLoader.getInstance().displayImage(history.getContent().getFigure(), holder.figure);
            holder.somename.setText(history.getContent().getName().toString() + "");
            holder.time.setText(history.getCreated_at()+"");
        }else
        if (history.getType().equals("test")) {
            holder.rootView.setVisibility(View.VISIBLE);
            holder.history_lishi.setVisibility(View.GONE);
   //         holder.delete.setVisibility(View.GONE);
            holder.history_point.setVisibility(View.VISIBLE);
            holder.plan_line.setImageResource(R.drawable.test_plan);
            if ( SharedPreferencesUtil.get(context, Constant.GENDER).equals("male")){
                holder.power.setText("俯卧撑");
            }else{
                holder.power.setText("仰卧起坐");
            }
            holder.defen.setText(history.getContent().getScore() + "");
            holder.test0.setText(history.getContent().getHeart() + "");
            holder.test1.setText(history.getContent().getBreath() + "");
            holder.test2.setText(history.getContent().getStrenth() + "");
            holder.time.setText(history.getCreated_at()+"");
        }else if (history.getType().equals("plan")){
            holder.rootView.setVisibility(View.VISIBLE);
            holder.plan_line.setImageResource(R.drawable.train_plan);
            holder.history_point.setVisibility(View.GONE);
            holder.figure.setVisibility(View.VISIBLE);
             //   holder.delete.setVisibility(View.GONE);
                holder.history_lishi.setVisibility(View.VISIBLE);
                holder.buju.setVisibility(View.VISIBLE);
                holder.text_coach.setVisibility(View.GONE);
                holder.text_style.setText("完成计划"+history.getContent().getCount());
                if(history.getContent().getFeedback()!=null){
                    if (history.getContent().getFeedback().equals("easy")){
                        holder.img_dongzuo.setImageResource(R.drawable.titleeasy);
                    }else if(history.getContent().getFeedback().equals("kind")){
                        holder.img_dongzuo.setImageResource(R.drawable.titlemid);
                    }else if(history.getContent().getFeedback().equals("trouble")){
                        holder.img_dongzuo.setImageResource(R.drawable.titletrouble);
                    }
                }else{
                    holder.img_dongzuo.setImageResource(R.drawable.titlemid);
                }
                ImageLoader.getInstance().displayImage(history.getContent().getFigure(), holder.figure, options);
                holder.somename.setText(history.getContent().getName().toString()+"");
            holder.time.setText(history.getCreated_at()+"");
        }else{
             holder.rootView.setVisibility(View.GONE);
        }
}catch (Exception r){}
        return convertView;
    }
    static class ViewHolder{
        TextView test0, test1, test2, time,defen,somename,text_style,text_coach,power;
        LinearLayout fuWO,history_point,history_lishi,buju;
        ImageView figure,plan_line,img_dongzuo;
        View rootView;
    }
}